﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class PostGraduation : System.Web.UI.Page
    {
        private int editid = 0;
        private int delid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterSchool"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (!IsPostBack)
            {
                if (editid > 0)
                {
                    BindData();
                    GraduationInfo graduationInfo = new GraduationInfo();
                    graduationInfo = Graduation.GetSingleInfo(editid);
                    this.Name.Text = graduationInfo.Name;
                    this.Literacy1.Text = graduationInfo.Literacy_One != null ? graduationInfo.Literacy_One.ToString() : "";
                    this.Literacy2.Text = graduationInfo.Literacy_Tow != null ? graduationInfo.Literacy_Tow.ToString() : "";
                    this.Literacy3.Text = graduationInfo.Literacy_Three != null ? graduationInfo.Literacy_Three.ToString() : "";
                    this.Science1.Text = graduationInfo.Science_One != null ? graduationInfo.Science_One.ToString() : "";
                    this.Science2.Text = graduationInfo.Science_Tow != null ? graduationInfo.Science_Tow.ToString() : "";
                    this.Science3.Text = graduationInfo.Science_Three != null ? graduationInfo.Science_Three.ToString() : "";
                    CheckBoxListHelper.SetSelectedValue(this.MidExamRoom, graduationInfo.MidExamRoomId.ToString());
                    CheckBoxListHelper.SetSelectedValue(this.HighExamRoom, graduationInfo.HighExamRoomId.ToString());
                }
                else
                {
                    BindData();
                }
                if (delid > 0)
                {
                    if (!UserValidate.CheckLogin(this.Request, "DelSchool"))
                    {
                        Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                        Response.Redirect("login.aspx");
                        Response.End();
                    }
                    Graduation.DelInfo(delid);
                    Response.Redirect("AdminExamRoom.aspx");
                    return;
                }
            }
        }
        private void BindData()
        {
            IList<ExamRoomInfo> examRoomInfos = ExamRoom.GetList();
            MidExamRoom.Items.Clear();
            foreach (ExamRoomInfo info in examRoomInfos)
            {
                MidExamRoom.Items.Add(new ListItem(info.ExamName.ToString(), info.Id.ToString()));
                HighExamRoom.Items.Add(new ListItem(info.ExamName.ToString(), info.Id.ToString()));
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (PageValidate.IsEmpty(this.Name.Text.Trim()))
            {
                Utility.ShowJsAlert("出入名称不能为空！", this);
                return;
            }
            if (PageValidate.IsEmpty(CheckBoxListHelper.SelectedValue(this.MidExamRoom)) || PageValidate.IsEmpty(CheckBoxListHelper.SelectedValue(this.HighExamRoom)))
            {
                Utility.ShowJsAlert("没有选择考试！", this);
                return;
            }
            GraduationInfo graduationInfo = null;
            if (editid > 0)
            {
                graduationInfo = Graduation.GetSingleInfo(editid);
                if (graduationInfo == null)
                {
                    Utility.ShowJsAlert("非法数据错误！", this);
                    return;
                }
            }
            else
            {
                graduationInfo = new GraduationInfo();
            }
            graduationInfo.Name = this.Name.Text.Trim();
            graduationInfo.Literacy_One = this.Literacy1.Text.Trim() == "" ? 0 : decimal.Parse(this.Literacy1.Text.Trim());
            graduationInfo.Literacy_Tow = this.Literacy2.Text.Trim() == "" ? 0 : decimal.Parse(this.Literacy2.Text.Trim());
            graduationInfo.Literacy_Three = this.Literacy3.Text.Trim() == "" ? 0 : decimal.Parse(this.Literacy3.Text.Trim());
            graduationInfo.Science_One = this.Science1.Text.Trim() == "" ? 0 : decimal.Parse(this.Science1.Text.Trim());
            graduationInfo.Science_Tow = this.Science2.Text.Trim() == "" ? 0 : decimal.Parse(this.Science2.Text.Trim());
            graduationInfo.Science_Three = this.Science3.Text.Trim() == "" ? 0 : decimal.Parse(this.Science3.Text.Trim());
            graduationInfo.MidExamRoomId = CheckBoxListHelper.SelectedValue(this.MidExamRoom);
            graduationInfo.HighExamRoomId = CheckBoxListHelper.SelectedValue(this.HighExamRoom);
            Graduation.Save(graduationInfo);
            Utility.ReloadTopPage(this);
        }
    }
}
